home *** CD-ROM | disk | FTP | other *** search
- /*
- ** QuickHelp macro
- ** Gives help on the word underneath the cursor
- ** First macro written by William B. McCormick
- */
-
-
- #define ALT_K 165
- #define FNAME "qhelp.$$$"
-
-
- init()
- {
- assign_key("qhelp", ALT_K);
- }
-
- /*
- ** WordChar:
- ** return TRUE if the character specified is an OK part of a word
- */
- WordChar(str)
- string str;
- {
- return ((str >= "a" && str <= "z") ||
- (str >= "A" && str <= "Z") ||
- (str >= "0" && str <= "9") ||
- (str == "_"));
- }
-
- /*
- ** Call QuickHelp to get help on the word underneath the cursor
- */
- qhelp()
- {
- string str;
- int first, last, len;
- int oldbuf, newbuf;
-
- /*
- * * save our position so we can go right back to where we * were before we
- * hit ALT_K * Don't forget to restore_position() before any return's
- */
- save_position();
-
- /* get the current line */
- str = currline();
-
- /*
- * * get the line length so we don't have to keep * computing it. This may
- * be no big deal.
- */
- len = strlen(str);
-
- /*
- * * move to beginning of current word by moving backwards until * we find
- * a character that doesn't belong in the word. * if the character we start
- * on doesn't belong, it's OK -- we'll * catch it after we find the last
- * character
- */
- for (first = currcol(); first > 1; --first)
- {
- if (!WordChar(substr(str, first, 1)))
- {
- ++first;
- break;
- }
- }
-
- /*
- * * move to end of current word * same method as before
- */
- for (last = currcol(); last <= len; ++last)
- {
- if (!WordChar(substr(str, last, 1)))
- {
- --last;
- break;
- }
- }
-
- /*
- * * here's where we catch the advance/retreat problems. If the *
- * character we started on doesn't belong in a word, first will * point to
- * the character after -- last will point to the * character before.
- * (Except in col 1 - I should fix that...)
- */
- if (last < first)
- {
- /* no word, so complain */
- message("No word under cursor");
- get_tty_char();
- restore_position();
- return;
- }
-
- /* get the word we're searching for */
- str = substr(str, first, last - first + 1);
-
- /* delete the file -- so we can tell if QH messes up */
- delete_file(FNAME);
-
- /* call QuickHelp to get the text */
- if (os_command(sprintf("qh %s -p %s -t all", str, FNAME)) < 0)
- {
- message("Can't call QuickHelp");
- get_tty_char();
- restore_position();
- return;
- }
-
- /* now display the help in a window */
-
- /* save the old buffer id (so we can go back) */
- oldbuf = currbuf();
-
- /* create a new buffer with the help text */
- newbuf = create_buffer(FNAME);
-
- /* and delete the file to keep the disk clean */
- delete_file(FNAME);
-
- setcurrbuf(newbuf);
-
- /*
- * * if no text in the window, QH messed up. Don't worry about an * error
- * message, QH took care of it
- */
- if (lastlinenum() <= 1)
- {
- delete_buffer(newbuf);
- setcurrbuf(oldbuf);
- restore_position();
- return;
- }
-
- /* display the help text */
- show_buffer(newbuf);
-
- /* display the old text */
- show_buffer(oldbuf);
-
- /* and go back to the same position */
- restore_position();
- return;
- }
-